package logic

import (
	"context"

	"mail/service/receipt/model"
	"mail/service/receipt/rpc/internal/svc"
	"mail/service/receipt/rpc/receipt"

	"github.com/zeromicro/go-zero/core/logx"
	"google.golang.org/grpc/status"
)

type AnnulLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAnnulLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AnnulLogic {
	return &AnnulLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AnnulLogic) Annul(in *receipt.AnnulRequest) (*receipt.AnnulResponse, error) {
	// todo: add your logic here and delete this line
	_, err := l.svcCtx.ReceiptModel.FindOne(l.ctx, in.Id)
	if err != nil {
		if err == model.ErrNotFound {
			return nil, status.Error(100, "receipt isn't exists.")
		}
		return nil, status.Error(500, err.Error())
	}
	err = l.svcCtx.ReceiptModel.Delete(l.ctx, in.Id)
	if err != nil {
		return nil, status.Error(500, err.Error())
	}
	return &receipt.AnnulResponse{}, nil
}
